package org.swidgets.sinputdata;

import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBoxBase;

public class FieldValidator 
{
	public static boolean txtEmpty(CalendarWidget field){

		return field.getDate().toString() ==null || field.getDate().toString().trim().length()==0;
	}

	public static boolean txtGreaterThan(TextBoxBase field,int size){
		return  field.getText().trim().length()>size;
	}

	public static boolean cfEmtyData(SInputData field){
		return field.isEmpty();
	}

	public static boolean cfInvalidData(SInputData field){
		return field.getDate()==null;
	}

	public static boolean txtInvalidData(CalendarWidget field){
		// Mi aspetto gg/mm/aaaa
		String txt = field.getDate().toString().trim();
		if (txt.length()!=10 && txt.length()!=8)
			return true;
		if (txt.charAt(2)!='/' || txt.charAt(5)!='/')
			return true;

		try{
			int giorno = Integer.parseInt(txt.substring(0,2));
			int mese   = Integer.parseInt(txt.substring(3,5));
			int anno   = Integer.parseInt(txt.substring(6));
			anno = Utilita.ctlYear(anno);

			int giorniAttesi = Utilita.getDaysInMonth(anno, mese);

			/*
			Date date = new Date();
			if (anno > date.getYear()+1900)
				return true;
			*/
			if (mese>12)
				return true;

			if (giorno >giorniAttesi)
				return true;
		}
		catch(Exception e){
			return true;
		}

		return false;
	}

	public static boolean selectionEmpty(ListBox field){
		return field.getSelectedIndex()==0;
	}

	public static boolean boxChecked(CheckBox field){
		return field.isChecked();
	}
}